﻿Public Class frmHistorySeach
    Private Sub txtLotNo_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtLotNo.KeyPress
        '頭に付いている'%'を消す
        Static Dim fIsPercentInput As Boolean = False
        If e.KeyChar = CChar("%") OrElse e.KeyChar = CChar("％") Then
            fIsPercentInput = True
            e.Handled = True
            Exit Sub
        End If
        If fIsPercentInput Then
            fIsPercentInput = False
            e.Handled = True
            Exit Sub
        End If

        If e.KeyChar = vbCr Then

            txtLotNo.Text = txtLotNo.Text.Trim.ToUpper

            Dim ds As DataTable

            ds = getData()
            dgvLot.DataSource = ds
            dgvLot.Refresh()
        End If
    End Sub


    Private Function getData() As DataTable

        Dim szSql As String = ""
        szSql &= "SELECT"
        szSql &= "  lot_no as KeyNo "
        szSql &= "  , koutei_cd as 工程"
        szSql &= "  , equip_id as 设备"
        szSql &= "  , case  "
        szSql &= " when type = '1' then '着工' "
        szSql &= " when type = '3' then '完工' "
        szSql &= " when type = '9' then '着工取消' "
        szSql &= " end as 类型"
        szSql &= "  , proc_times as 回数 "
        szSql &= "  , begin_time  as 开始时间 "
        szSql &= "  , input_qty  as 投入数量 "
        szSql &= "  , complete_qty  as 完成数量"
        szSql &= "  , defect_qty  as 不良数"
        szSql &= "  , inputfrm_qty  as 入力框架数" 'UPD MIAO 2018/10/30
        szSql &= "  , frame_qty  as 出力框架数" 'UPD MIAO 2018/10/30
        szSql &= "  , inputcross_qty  as 入力X印数" 'UPD MIAO 2018/10/30
        szSql &= " , cross_qty as 出力X印数" 'UPD MIAO 2018/10/30
        szSql &= "  , inputcross_frmqty  as 入力X印枚数" 'UPD MIAO 2018/10/30
        szSql &= "  , cross_frmqty  as 出力X印枚数" 'UPD MIAO 2018/10/30
        szSql &= " FROM  mat.l_sf_work_rslt_head "
        szSql &= " WHERE  LOT_NO = ? "
        szSql &= " ORDER BY  BEGIN_TIME ASC  , TYPE ASC"

        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, txtLotNo.Text.Trim.ToUpper)
        If IsNothing(dt) Then
            getData = New DataTable
        Else
            getData = dt
        End If
    End Function


    Private Sub txtLotNo_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles txtLotNo.PreviewKeyDown
        'タブキーが押下された場合は、改行コードと見なし処理を行う
        If e.KeyCode = Keys.Tab Then
            e.IsInputKey = True
            txtLotNo_KeyPress(sender, New KeyPressEventArgs(vbCr))
        End If
    End Sub

    Private Sub dgvLot_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvLot.CellContentClick

    End Sub

    Private Sub dgvLot_CellContextMenuStripChanged(sender As Object, e As DataGridViewCellEventArgs) Handles dgvLot.CellContextMenuStripChanged

    End Sub

    Private Sub dgvLot_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvLot.CellClick

        Dim szSql As String = ""
        Dim strProcCD As String = ""
        Dim strProcCDTable As String


        For n = 0 To gb1.Controls.Count - 1
            If gb1.Controls.Item(n).Name <> "lblRack1" Then
                gb1.Controls(n).Text = ""
                gb1.Controls(n).Enabled = False
            End If
        Next
        For n = 0 To gb2.Controls.Count - 1
            If gb2.Controls.Item(n).Name <> "lblRack2" Then
                gb2.Controls(n).Text = ""
                gb2.Controls(n).Enabled = False
            End If
        Next

        strProcCD = dgvLot.CurrentRow.Cells("工程").Value

        szSql &= "SELECT proc_table FROM per.t_proc_cd WHERE proc_cd = ? "
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, strProcCD)
        If IsNothing(dt) Then
            Exit Sub
        End If
        Dim txtData As New TextBox

        strProcCDTable = dt.Rows(0)(0)
        dt = Nothing
        szSql = ""
        szSql &= " Select actual_model from mat." & strProcCDTable
        szSql &= "  where"
        szSql &= " (mnf_cnd_tag = 'ACCEPTED MS RACK NO' or mnf_cnd_tag = 'WAFER CASE NO'  or mnf_cnd_tag = 'ACCEPTED RACK NO' "
        If strProcCD = "A801" Or strProcCD = "A802" Or strProcCD = "A806" Then
            szSql &= " Or mnf_cnd_tag = 'RACK NO') "
        Else
            szSql &= ") "
        End If
        szSql &= " And Type = '1' "
        szSql &= " And lot_no = ? "
        szSql &= " And proc_times = ?"

        dt = DBacs.ExecuteSql(szSql, -1, txtLotNo.Text.Trim.ToUpper, dgvLot.CurrentRow.Cells("回数").Value)
        If IsNothing(dt) = False Then
            For i = 0 To dt.Rows.Count() - 1
                gb1.Controls.Find(String.Format("txtRackACCEPTED0{0}", i + 1), False)(0).Text = dbnulltoblank(dt(i)(0)) 'UPD MIAO 2018/10/30
            Next
        End If

        dt = Nothing
        szSql = ""
        szSql &= " Select actual_model from  mat." & strProcCDTable
        szSql &= "  where"
        szSql &= " (mnf_cnd_tag = 'COMP MS RACK NO' or mnf_cnd_tag = 'COMP RACK NO'"
        If strProcCD = "S41" Or strProcCD = "A41" Or strProcCD = "A801" Or strProcCD = "A802" Or strProcCD = "A806" Then
            szSql &= " Or mnf_cnd_tag = 'RACK NO') "
        Else
            szSql &= " Or mnf_cnd_tag = 'WAFER CASE NO') "
        End If

        szSql &= " And Type = '3' "
        szSql &= " And lot_no = ? "
        szSql &= " And proc_times = ?"

        dt = DBacs.ExecuteSql(szSql, -1, txtLotNo.Text.Trim.ToUpper, dgvLot.CurrentRow.Cells("回数").Value)
        If IsNothing(dt) = False Then
            For i = 0 To dt.Rows.Count() - 1
                gb2.Controls.Find(String.Format("txtComp0{0}", i + 1), False)(0).Text = DBNullTOblank(dt(i)(0)) '2018/10/30
            Next
        End If

    End Sub
    'ADD MIAO 2018/10/17 Begin
    Public Function DBNullTOblank(obj As Object) As String
        DBNullTOblank = IIf(IsDBNull(obj), "", obj)
    End Function
    'ADD MIAO 2018/10/17 END
End Class